﻿/********************************************************************************/
// <copyright file="ApplicationConfigRepository.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="17/12/2012 3:37:34 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Infrastructure.Repositories.ApplicationConfig
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class ApplicationConfigRepository : SQLRepositoryBase<Model.ApplicationConfig, Guid>, IApplicationConfigRepository
    {
        public override Model.ApplicationConfig FindBy(Guid key)
        {
            var config = DataContext.ApplicationConfigs.SingleOrDefault(x => key.Equals(x.ID));
            if (config != null)
            {
                return ApplicationConfigFactory.FromLINQObject(config);
            }
            else
            {
                return null;
            }
        }

        public Model.ApplicationConfig FindByConfigKey(string key)
        {
            var config = DataContext.ApplicationConfigs.SingleOrDefault(x => key.Equals(x.Key));
            if (config != null)
            {
                return ApplicationConfigFactory.FromLINQObject(config);
            }
            else
            {
                return null;
            }
        }

        public override IList<Model.ApplicationConfig> FindAll()
        {
            var configs = DataContext.ApplicationConfigs.ToList();
            if (configs != null)
            {
                var allApplicationConfigs = new List<Model.ApplicationConfig>();
                foreach (var c in configs)
                {
                    allApplicationConfigs.Add(ApplicationConfigFactory.FromLINQObject(c));
                }
                return allApplicationConfigs;
            }
            else
            {
                return new List<Model.ApplicationConfig>();
            }
        }

        protected override void PersistNewItem(Model.ApplicationConfig item)
        {
            DataContext.ApplicationConfigs.InsertOnSubmit(ApplicationConfigFactory.ToLINQObject(item));
        }

        protected override void PersistUpdatedItem(Model.ApplicationConfig item)
        {
            var config = DataContext.ApplicationConfigs.SingleOrDefault(x => item.ID.Equals(x.ID) &&
                                                                             item.SettingValue != x.SettingValue);
            if (config != null)
            {
                config.SettingValue = item.SettingValue;
            }
        }

        protected override void PersistDeletedItem(Model.ApplicationConfig item)
        {
            var config = DataContext.ApplicationConfigs.SingleOrDefault(x => item.ID.Equals(x.ID) && item.Key.Equals(x.Key));
            if (config != null)
            {
                DataContext.ApplicationConfigs.DeleteOnSubmit(config);
            }
        }
    }
}
